import { createApp } from 'vue'
import { createPinia } from 'pinia'
// import { Grid, GridItem } from 'vant';
// import { Cell, CellGroup } from 'vant';
import store from '@/stores'
import { Toast } from 'vant'

import App from './App.vue'
import router from './router'

import 'normalize.css'

// const token = localStorage.getItem('TOKEN')
// console.log(token);

const app = createApp(App)

router.beforeEach((to, from, next) => {
   
        if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
            if (Boolean(localStorage.getItem("TOKEN"))) { // 通过浏览器本地缓存判断当前的token是否存在              
                next();
            } else {
                Toast('请先登录！')
                next({
                    path: '/login',
                    query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
                })
            }
        } else {
            if (Boolean(localStorage.getItem("token"))) { // 判断是否登录
                if (to.path != "/" && to.path != "/login") { //判断是否要跳到登录界面
                    next();
                } else {
                    /**
                     * 防刷新，如果登录，修改路由跳转到登录页面，修改路由为登录后的首页
                     */
                    next({
                        path: '/index'
                    })
                }
            } else {
                next();
            }
        }
})


app.use(createPinia())
app.use(router)
app.use(store)
// app.use(Grid);
// app.use(GridItem);
// app.use(Cell);
// app.use(CellGroup);

app.mount('#app')
